package com.xyq.fs.task;

import javafx.application.Platform;

public abstract class AfAsyncTask extends Thread {

	public int status = 0;
	public String reason;
	private boolean needUpdateUi;

	public boolean isNeedUpdateUi() {
		return needUpdateUi;
	}

	public void setNeedUpdateUi(boolean needUpdateUi) {
		this.needUpdateUi = needUpdateUi;
	}

	public AfAsyncTask(boolean needUpdateUi) {

		this.needUpdateUi = needUpdateUi;
	}

	public void execute() {
		this.start();
	}

	@Override
	public void run() {

		/* 执行任务 */
		status = 1;
		try {
			doInBackground();
			status = 100;
		} catch (Exception e) {
			status = -1;
			reason = e.getMessage();
			e.printStackTrace();
		}

		if (needUpdateUi)
			/* 把结果显示到UI */
			Platform.runLater(new Runnable() {
				@Override
				public void run() {
					onPostExecute();
				}
			});
	}

	protected abstract void doInBackground() throws Exception;

	protected abstract void onPostExecute();

}
